﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NMF.Optimizations.Optimizer
{
    public abstract class OptimizerBase : IOptimizer
    {
        public IOptimizationScenario Optimize(IOptimizationScenario initialScenario, IScenarioEvaluator evaluator)
        {
            if (initialScenario == null) throw new ArgumentNullException("initialScenario");
            if (evaluator == null) throw new ArgumentNullException("evaluator");

            initialScenario.Cost = evaluator.EvaluateScenario(initialScenario);
            var bestScenario = FindBestScenario(initialScenario, evaluator);

            bool suceed;
            foreach (var mod in bestScenario.AppliedModifications)
            {
                suceed = mod.Apply();
                if (!suceed) throw new InvalidOperationException("The optimization was cancelled. Check the reversibility of your modifications.");
            }
            return bestScenario;
        }

        protected abstract IOptimizationScenario FindBestScenario(IOptimizationScenario initialScenario, IScenarioEvaluator evaluator);
    }
}
